package com.huiyujinrong.qiangdanbao.utils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Rect;
import android.net.Uri;
import android.os.SystemClock;
import android.provider.MediaStore;
import android.text.TextUtils;
import android.view.View;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class ScreenShot {

    public static Bitmap takeScreenShot(Activity activity) {
        View view = activity.getWindow().getDecorView();
        view.setDrawingCacheEnabled(true);
        view.buildDrawingCache();
        Bitmap b1 = view.getDrawingCache();

        Rect frame = new Rect();
        activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
        int statusBarHeight = frame.top;
        System.out.println(statusBarHeight);
        int width = activity.getWindowManager().getDefaultDisplay().getWidth();
        int height = activity.getWindowManager().getDefaultDisplay().getHeight();
        Bitmap b = Bitmap.createBitmap(b1, 0, statusBarHeight, width, height - statusBarHeight);
        view.destroyDrawingCache();
        return b;

    }

    public static Bitmap takeScreenShotClip(Activity activity, int x, int y, int width, int height) {
        View view = activity.getWindow().getDecorView();
        view.setDrawingCacheEnabled(true);
        view.buildDrawingCache();
        Bitmap b1 = view.getDrawingCache();

        Rect frame = new Rect();
        activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
        int statusBarHeight = frame.top;
        System.out.println(statusBarHeight);
        Bitmap b = Bitmap.createBitmap(b1, x, y, width, height);
        view.destroyDrawingCache();
        return b;

    }

    public static String savePic(Context context, Bitmap b) {
        String mLocalUrl = "";
        boolean sucess = false;
        File file = null;

        if (b != null) {
            if (TextUtils.isEmpty(mLocalUrl)) {
                mLocalUrl = context.getExternalFilesDir("Screenshots").getAbsoluteFile() + "/" + SystemClock.currentThreadTimeMillis() + ".png";
            }
            file = new File(mLocalUrl);
            if (!file.exists()) {
                try {
                    file.createNewFile();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            FileOutputStream fos = null;
            try {
                fos = new FileOutputStream(mLocalUrl);
                if (null != fos) {
                    b.compress(Bitmap.CompressFormat.PNG, 100, fos);
                    fos.flush();
                    fos.close();
                    sucess = true;
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
                file = null;
            } catch (IOException e) {
                e.printStackTrace();
                file = null;
            }

            try {
                // 其次把文件插入到系统图库
                MediaStore.Images.Media.insertImage(context.getContentResolver(), file.getAbsolutePath(), mLocalUrl, null);
                // 最后通知图库更新
                context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + mLocalUrl)));
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }

        }

        return mLocalUrl;
    }
}
